package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.utils.CurrentHolder;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //根据分类id查询套餐
    @Override
    public List<Setmeal> getList(Setmeal setmeal) {
       List list = setmealMapper.getList(setmeal);
       return list;
    }

    //根据套餐id查询套餐的菜品详情
    @Override
    public List<DishItemVO> setmealDish(String setmealId) {
        List<DishItemVO> dishList = setmealDishMapper.getDishList(setmealId);
        return dishList;
    }

    //分页查询
    @Override
    public PageResult getSetmealList(int page, int pageSize, String name, Integer status, Integer categoryId) {
        PageHelper.startPage(page, pageSize);
        //查询
        List<SetmealVO> setmealList =setmealMapper.getSetmealList(name, status, categoryId);
        Page<SetmealVO> page1 = (Page<SetmealVO>) setmealList;
        return new PageResult(page1.getTotal(), page1.getResult());
    }

    //新增套餐
    @Override
    public void save(SetmealDTO setmealDTO) {
        //新增套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(CurrentHolder.getCurrentId());
        setmeal.setUpdateUser(CurrentHolder.getCurrentId());
        setmealMapper.saveSetmeal(setmeal);
        //新增套餐和菜品的关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.saveSetmealDish(setmealDishes);

    }

    //根据套餐id查询套餐数据
    @Override
    public SetmealVO getById(String id) {
        //查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        //查询套餐中的菜品数据
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishById(setmealId);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //修改套餐
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateUser(CurrentHolder.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //删除当前套餐对应的菜品数据
        //先判断套餐的菜品表中有没有数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)&& setmealDishes.size()>0){
            //清空套餐中菜品
            setmealDishMapper.deleteById(setmealId);
            //绑定菜品的套餐id
            setmealDishes.forEach(dish -> dish.setSetmealId(setmealId));
           //添加套餐中的菜品数据
            setmealDishMapper.saveSetmealDish(setmealDishes);

        }

    }

    //删除套餐
    @Override
    public void delete(List<Long> ids) {
        //删除套餐
        setmealMapper.delete(ids);
        //删除套餐中的菜品数据
        setmealDishMapper.delete(ids);
    }

    //起售停售
    @Override
    public void startOrStop(String status, String id) {
        Integer stt = Integer.valueOf(status);
        Long id2 = Long.valueOf(id);
        setmealMapper.setStatus(stt,id2);
    }


}
